package com.lancoo.ccas52.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas52.aspect.RateLimiter;
import com.lancoo.ccas52.entity.CssTaskInfo;
import com.lancoo.ccas52.entity.Student;
import com.lancoo.ccas52.pojo.common.PageInfo;
import com.lancoo.ccas52.pojo.dto.CssTaskRoundInfo;
import com.lancoo.ccas52.pojo.vo.*;
import com.lancoo.ccas52.service.CssTaskInfoService;
import com.lancoo.ccas52.service.StudentService;
import com.lancoo.ccas52.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName StudentController
 * @Description 学生选课-信息
 * @Author quin
 * @Date 2023/2/3 13:52
 */
@Api(tags = "学生选课")
@RestController
@RequestMapping("/api/v5.2.1/pc/student")
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private CssTaskInfoService cssTaskInfoService;

    @ApiOperation("获取全部学生")
    @GetMapping(value = "/getStudentList")
    public ResponseObject<PageInfo<Student>> getStudentByConditions(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                    @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                    @ApiParam(value = "专业id ", required = false) @RequestParam(required = false) String majorId,
                                                                    @ApiParam(value = "年级id ", required = false) @RequestParam(required = false) String gradeId,
                                                                    @ApiParam(value = "班级id ", required = false) @RequestParam(required = false) String classId,
                                                                    @ApiParam(value = "名称、学号模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                    @ApiParam(value = "选课范围 1本专业年级  2本专业跨年级 3本院系跨年级 4全部专业年级 ", required = false) @RequestParam(required = false) Integer selectionScope,
                                                                    @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                    @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<Student> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(studentService.getStudentByConditions(taskId, page, collegeId, majorId, gradeId, classId, keyWord, selectionScope));
    }

    @ApiOperation("获取指定行政班学生")
    @PostMapping(value = "/getStudentByClassIds")
    public ResponseObject<List<Student>> getStudentByConditions(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                @ApiParam(value = "班级ids ", required = true) @RequestBody List<String> classIds) {
        List<Student> studentList = new ArrayList<>();
        if (CollUtil.isEmpty(classIds)) {
            return ResponseObject.ofSuccess(studentList);
        }
        studentList = studentService.lambdaQuery().in(Student::getClassId, classIds).eq(Student::getTaskId, taskId).list();
        return ResponseObject.ofSuccess(studentList);
    }


    @ApiOperation("获取选课任务")
    @PostMapping(value = "/getCssTask")
    public ResponseObject<List<CssTaskRoundInfo>> getCssTask(@ApiParam(value = "获取选课计划入参实体类", required = true) @RequestBody GetCssTask getCssTask) {
        return ResponseObject.ofSuccess(cssTaskInfoService.getCssTaskBySchool(getCssTask));
    }

    @ApiOperation("根据基础平台学生Id获取学生信息")
    @GetMapping(value = "/getStudentInfoByBaseStudentId")
    public ResponseObject<Student> getStudentInfoByBaseStudentId(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "基础平台学生id ", required = true) @RequestParam String baseStudentId) {
        return ResponseObject.ofSuccess(studentService.getStudentByBaseStudentId(taskId, baseStudentId));
    }

    @ApiOperation("获取学生已选课程")
    @GetMapping(value = "/preSelected")
    public ResponseObject<List<StudentTeachingClass>> getSelectedTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                               @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId,
                                                                               @ApiParam(value = "0无需选课 1在线选课;2:分级教学 3:预选教学班", required = true) @RequestParam Integer teachingClassType) {
        return ResponseObject.ofSuccess(studentService.getSelectedByStudentId(taskId, studentId, teachingClassType));
    }

    @ApiOperation("判断学生是否是禁选学生")
    @GetMapping(value = "/isProhibitStudent")
    public ResponseObject<Boolean> isProhibitStudent(@ApiParam(value = "学生id ", required = true) @RequestParam Long studentId) {
        return ResponseObject.ofSuccess(studentService.isProhibitStudent(studentId));
    }

    @ApiOperation("学生选课")
    @PostMapping(value = "/selectCourse")
    public ResponseObject<Object> selectCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                               @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId,
                                               @ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId,
                                               @ApiParam(value = "退选教学班id", required = false) @RequestParam(required = false) Long cancelTeachingClassId) {
        int res = studentService.selectCourse(taskId, studentId, teachingClassId, cancelTeachingClassId);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("选课成功");
            case -1:
                return ResponseObject.ofFail(2, "选课已满，无法继续选课");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -3:
                return ResponseObject.ofFail(5, "已添加相同课程下的教学班，请勿重复选择");
            case -4:
                return ResponseObject.ofFail(2, "不存在该教学班");
            default:
                return ResponseObject.ofFail(2, "选课失败，请重试");
        }
    }

    @ApiOperation("获取学生选课数据")
    @PostMapping(value = "/getStudentTeachingClassView")
    public ResponseObject<StudentTeachingClassView> getAvailableClass(@ApiParam(value = "学生筛选数据入参", required = true) @RequestBody GetAvailableCondition getAvailableCondition) {
        return ResponseObject.ofSuccess(studentService.getAvailableTeachingClass(getAvailableCondition));
    }

    @ApiOperation("获取无需选课的课程 环节教学班")
    @GetMapping(value = "/getNormalTeachingClass")
    public ResponseObject<List<StudentTeachingClass>> getNormalTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                             @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId) {
        return ResponseObject.ofSuccess(studentService.getNormalTeachingClass(taskId, studentId));
    }

    @ApiOperation("学生选课2")
    @PostMapping(value = "/selectCourseOptimize")
    public ResponseObject<Object> selectCourseOptimize(@ApiParam(value = "选课实体类", required = true) @RequestBody AddSelectCourse addSelectCourse) {
        int res = studentService.selectCourseOptimize(addSelectCourse);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("选课成功");
            case -1:
                return ResponseObject.ofFail(2, "选课已满，无法继续选课");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -3:
                return ResponseObject.ofFail(5, "已添加相同课程下的教学班，请勿重复选择");
            case -4:
                return ResponseObject.ofFail(2, "不存在该教学班");
            default:
                return ResponseObject.ofFail(2, "系统处理异常，选课失败");
        }
    }

    @ApiOperation("退选2")
    @PostMapping(value = "/cancelCourseOptimize")
    public ResponseObject<String> cancelCourseOptimize(@ApiParam(value = "退选实体类", required = true) @RequestBody AddCancelCourse addCancelCourse) {
        int res = studentService.cancelCourseOptimize(addCancelCourse);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("退选成功");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -4:
                return ResponseObject.ofFail(2, "不存在该教学班");
            default:
                return ResponseObject.ofFail(2, "系统处理异常，退选失败");
        }
    }


    @ApiOperation("退选")
//    @RateLimiter(qps = 1)
    @PostMapping(value = "/cancelCourse")
    public ResponseObject<String> cancelCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                               @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId,
                                               @ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId,
                                               @ApiParam(value = "2退选   3管理员移除", required = true) @RequestParam Integer flag) {
        int res = studentService.cancelCourse(taskId, studentId, teachingClassId, flag);
        switch (res) {
            case 1:
                return ResponseObject.ofSuccess("退选成功");
            case -2:
                return ResponseObject.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -4:
                return ResponseObject.ofFail(2, "不存在该教学班");
            default:
                return ResponseObject.ofFail(2, "退选失败，请重试");
        }
    }

    @ApiOperation("判断是否选择该课程")
    @GetMapping(value = "/isExistSelectCourse")
    public ResponseObject<StudentTeachingClass> isExistSelectCourse(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                    @ApiParam(value = "学生id ", required = true) @RequestParam Long studentId,
                                                                    @ApiParam(value = "教学班类型 1在线选课 3预选", required = true) @RequestParam Integer teachingClassType,
                                                                    @ApiParam(value = "1主修 2辅修", required = true) @RequestParam Integer studyNature,
                                                                    @ApiParam(value = "课程id", required = true) @RequestParam Long courseId) {
        return ResponseObject.ofSuccess(studentService.isExistSelectCourse(taskId, studentId, teachingClassType, studyNature, courseId));

    }

    @ApiOperation("清空学生选课缓存")
    @DeleteMapping(value = "/cacheStudent")
    public ResponseObject<Boolean> syncTeachingClassStudent(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(studentService.syncTeachingClassStudent(taskId));
    }

    @ApiOperation("清空学生对应教学班缓存")
    @DeleteMapping(value = "/cacheTeachingClass")
    public ResponseObject<Boolean> cacheTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "课程id", required = true) @RequestParam Long courseId,
                                                      @ApiParam(value = "修读性质：1主修2辅修", required = true) @RequestParam Integer studyNature,
                                                      @ApiParam(value = "教学班id", required = true) @RequestParam Long teachingClassId) {
        return ResponseObject.ofSuccess(studentService.clearCacheByTeachingClass(taskId, courseId, studyNature, teachingClassId));
    }


}
